#include "bms_Slave.h"
#include "rtwtypes.h"
#include "Alarm_Recov_sys.h"
#include "bms_Slave_private.h"

#define bms_Slave_IN_Err               ((uint8_T)1U)
#define bms_Slave_IN_NO_ACTIVE_CHILD   ((uint8_T)0U)
#define bms_Slave_IN_Normal            ((uint8_T)2U)

#define bms_Slave_IN_Err_k             ((uint8_T)1U)
#define bms_Slave_IN_Normal_m          ((uint8_T)2U)

void bms_Slave_Alarm_Recov_sys_Init(boolean_T *rty_Alarm_flg)
{
  *rty_Alarm_flg = false;
}

void bms_Slave_Alarm_Recov_sys_Reset(boolean_T *rty_Alarm_flg,
  DW_Alarm_Recov_sys_bms_Slave_T *localDW)
{
  localDW->is_active_c103_Bms_lib = 0U;
  localDW->is_c103_Bms_lib = bms_Slave_IN_NO_ACTIVE_CHILD;
  localDW->count = 0U;
  *rty_Alarm_flg = false;
}

void bms_Slave_Alarm_Recov_sys(boolean_T rtu_Err_flg, boolean_T rtu_Recov_flg,
  uint16_T rtu_Alarm_time, uint16_T rtu_Recov_time, boolean_T *rty_Alarm_flg,
  DW_Alarm_Recov_sys_bms_Slave_T *localDW)
{
  if (localDW->is_active_c103_Bms_lib == 0U) {
    localDW->is_active_c103_Bms_lib = 1U;
    localDW->is_c103_Bms_lib = bms_Slave_IN_Normal;
    localDW->count = 1U;
  } else if (localDW->is_c103_Bms_lib == bms_Slave_IN_Err) {
    if (rtu_Recov_flg) {
      localDW->is_c103_Bms_lib = bms_Slave_IN_Normal;
      localDW->count = 1U;
    } else {
      if (localDW->count >= rtu_Alarm_time) {
        *rty_Alarm_flg = true;
        localDW->count = rtu_Alarm_time;
      }

      if (rtu_Err_flg) {
        uint32_T tmp;
        tmp = localDW->count + 1U;
        if (localDW->count + 1U > 65535U) {
          tmp = 65535U;
        }

        localDW->count = (uint16_T)tmp;
      }

      if ((!rtu_Err_flg) && (localDW->count < rtu_Alarm_time)) {
        localDW->count = 1U;
      }
    }
  } else if (rtu_Err_flg) {
    localDW->is_c103_Bms_lib = bms_Slave_IN_Err;
    localDW->count = 1U;
  } else {
    if (localDW->count >= rtu_Recov_time) {
      *rty_Alarm_flg = false;
      localDW->count = rtu_Recov_time;
    }

    if (rtu_Recov_flg) {
      uint32_T tmp;
      tmp = localDW->count + 1U;
      if (localDW->count + 1U > 65535U) {
        tmp = 65535U;
      }

      localDW->count = (uint16_T)tmp;
    }

    if ((!rtu_Recov_flg) && (localDW->count < rtu_Recov_time)) {
      localDW->count = 1U;
    }
  }
}

void Alarm_Recov_sys_Init(boolean_T *rty_Alarm_flg)
{
  *rty_Alarm_flg = false;
}

void Alarm_Recov_sys(boolean_T rtu_Err_flg, boolean_T rtu_Recov_flg, uint16_T
                     rtu_Alarm_time, uint16_T rtu_Recov_time, boolean_T
                     *rty_Alarm_flg, DW_Alarm_Recov_sys_T *localDW)
{
  if (localDW->is_active_c36_bms_Slave == 0U) {
    localDW->is_active_c36_bms_Slave = 1U;
    localDW->is_c36_bms_Slave = bms_Slave_IN_Normal_m;
    localDW->count = 1U;
  } else if (localDW->is_c36_bms_Slave == bms_Slave_IN_Err_k) {
    if (rtu_Recov_flg) {
      localDW->is_c36_bms_Slave = bms_Slave_IN_Normal_m;
      localDW->count = 1U;
    } else {
      uint32_T tmp;
      tmp = localDW->count + 1U;
      if (localDW->count + 1U > 65535U) {
        tmp = 65535U;
      }

      localDW->count = (uint16_T)tmp;
      if (localDW->count >= rtu_Alarm_time) {
        *rty_Alarm_flg = true;
        localDW->count = rtu_Alarm_time;
      }
    }
  } else if (rtu_Err_flg) {
    localDW->is_c36_bms_Slave = bms_Slave_IN_Err_k;
    localDW->count = 1U;
  } else {
    uint32_T tmp;
    tmp = localDW->count + 1U;
    if (localDW->count + 1U > 65535U) {
      tmp = 65535U;
    }

    localDW->count = (uint16_T)tmp;
    if (localDW->count >= rtu_Recov_time) {
      *rty_Alarm_flg = false;
      localDW->count = rtu_Recov_time;
    }
  }
}
